package com.rongshu.sendmessage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.bean.LocationInfo;

import java.util.List;

public class AddressAdapter extends BaseAdapter {
    private List<LocationInfo> mList;
    private final Context mContext;
    private final LayoutInflater mInflater;

    public AddressAdapter(Context context, List<LocationInfo> list) {
        mList=list;
        mContext=context;
        mInflater= LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView==null){
            holder=new Holder();
            convertView=mInflater.inflate(R.layout.location_item,null);
            holder.iv_selector=convertView.findViewById(R.id.iv_selector);
            holder.address_title=convertView.findViewById(R.id.address_title);
            holder.address_des=convertView.findViewById(R.id.address_des);
            convertView.setTag(holder);
        }else{
            holder= (Holder) convertView.getTag();
        }
        LocationInfo locationInfo = mList.get(position);
        if (locationInfo.isSelect()){
            holder.iv_selector.setVisibility(View.VISIBLE);
        }else{
            holder.iv_selector.setVisibility(View.INVISIBLE);
        }
        holder.address_title.setText(locationInfo.getTitle());
        holder.address_des.setText(locationInfo.getAddress());
        return convertView;
    }

    public void setList(List<LocationInfo> list) {
        mList=list;
    }

    private class Holder{

        public ImageView iv_selector;
        public TextView address_title;
        public TextView address_des;
    }
}
